﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class Common_Com
    {
      
        #region 图片转换
        /// <summary>
        /// 图片转base64进制
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeObjevtToString(object obj)
        {
            BinaryFormatter binary = new BinaryFormatter();
            string result = string.Empty;
            using (MemoryStream stream = new MemoryStream())
            {
                binary.Serialize(stream, obj);
                byte[] bytes = stream.ToArray();
                //将字节数组转化成base64字符串
                result = Convert.ToBase64String(bytes);
                stream.Flush();
            }
            return result;
        }
        /// <summary>
        /// base64进制转图片
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object DeSerializeObjevtToString(string str)
        {
            //二级制格式
            BinaryFormatter binary = new BinaryFormatter();
            //空对象
            object result = null;
            //将字符串转为object
            byte[] bytes = Convert.FromBase64String(str);
            //创建流对象
            using (MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length))
            {
                //反序列化
                result = binary.Deserialize(stream);
            }
            return result;
        }
        #endregion
    }
}
